package cc.vv.baselibrary.view.imgselector.album;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;

import cc.vv.baselibrary.R;
import cc.vv.baselibrary.view.BaseTopBarView;
import cc.vv.baselibrary.view.imgselector.album.previewimage.ImageDetailFragment;
import cc.vv.baselibrary.view.imgselector.base.ImageSelectorActivity;
import cc.vv.baselibrary.view.imgselector.model.AlbumRepository;
import cc.vv.baselibrary.view.imgselector.utils.ActivityUtils;


public class AlbumActivity extends ImageSelectorActivity {

    public static final String TAG = AlbumActivity.class.getSimpleName();

    private AlbumPresenterImage mAlbumPresenter;

    private BaseTopBarView mSubmitBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.galleryselect_activity_album);
        initViews();
        AlbumFragment albumFragment;
        ImageDetailFragment imageDetailFragment;

        if (savedInstanceState != null) {   //内存重启时调用   解决 内存重启时可能发生的Fragment重叠异常
            imageDetailFragment = (ImageDetailFragment) getSupportFragmentManager().findFragmentByTag(ImageDetailFragment.TAG);

            albumFragment = (AlbumFragment) getSupportFragmentManager().findFragmentByTag(AlbumFragment.TAG);


            if (imageDetailFragment != null && albumFragment != null) {
                getSupportFragmentManager().beginTransaction()
                        .hide(albumFragment)
                        .show(imageDetailFragment)
                        .commit();
            }
        } else {
            //创建AlbumFragment

            albumFragment = AlbumFragment.newInstance();
            ActivityUtils.addFragmentToActivity(
                    getSupportFragmentManager(), albumFragment, AlbumFragment.TAG, false);

        }

        albumFragment.setPos(pos);

        AlbumRepository albumRepository = AlbumRepository.getInstance(this);
        //创建AlbumPresenter
        mAlbumPresenter = new AlbumPresenterImage(
                albumRepository,
                getSupportLoaderManager(),
                albumFragment);
    }

    private int pos;


    private void initViews() {
        if (null != getIntent()) {
            pos = getIntent().getIntExtra("pos", 0);
        }
        mSubmitBtn = findViewById(R.id.jtb_alb_top_bar);
        mSubmitBtn.setTitle("图片");
        mSubmitBtn.setRightTVColor(ContextCompat.getColor(this, R.color.color_EF7726));
        mSubmitBtn.setEventInterface(new BaseTopBarView.EventInterface() {
            @Override
            public void leftOnClick() {
                onBackPressed();

            }

            @Override
            public void leftCloseClick() {

            }

            @Override
            public void rightOnClick() {
                mAlbumPresenter.returnResult();
            }
        });

    }

    public void setSubmitBtnText(CharSequence text, boolean enabled) {
        mSubmitBtn.setRightTVContent(text.toString());
        mSubmitBtn.setEnabled(enabled);
    }

    public void setToolbarTitle(CharSequence title) {

    }

    @Override
    public void onBackPressed() {
        AlbumFragment albumFragment =
                (AlbumFragment) getSupportFragmentManager().findFragmentByTag(AlbumFragment.TAG);

        if (albumFragment == null) {
            super.onBackPressed();
            return;
        }

        if (albumFragment.isHidden()) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .show(albumFragment)
                    .commit();
        } else if (albumFragment.mFab.getVisibility() != View.VISIBLE) {
            albumFragment.hideFolderList();
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAlbumPresenter.clearCache();
    }

    @Override
    public void onConfigurationChanged(Configuration config) {
        super.onConfigurationChanged(config);
    }
}
